package com.lhy.tool.patchplugin.strategy;

import com.lhy.tool.patchplugin.PatchPluginProperties;
import com.lhy.tool.patchplugin.model.CompileFileWrapper;
import com.lhy.tool.patchplugin.model.SrcFileWrapper;
import com.lhy.tool.patchplugin.model.TargetFileWrapper;
import com.lhy.tool.patchplugin.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hyluan
 * @ClassName: SimpleFileStrategy
 * @Description: 简单文件策略
 * 从配置文件中获取绝对路径 复制到目标绝对路径
 * @date 2018/4/23 11:33
 * @Copyright: Copyright (c) 2018 wisedu
 */
public class SimpleFileStrategy implements FileStrategy {

    /**
     * 目标文件夹路径
     */
    protected List<String> targetForderPaths;

    @Autowired
    protected PatchPluginProperties patchPluginProperties;

    public SimpleFileStrategy(List<String> targetForderPaths) {
        if(patchPluginProperties == null){
            this.patchPluginProperties = SpringUtil.getBean(PatchPluginProperties.class);
        }
        this.targetForderPaths = targetForderPaths;
    }

    @Override
    public boolean accept(SrcFileWrapper srcFileWrapper) {
        return !srcFileWrapper.getAbsolutePath().startsWith(patchPluginProperties.getSrcPath());
    }

    @Override
    public List<CompileFileWrapper> getCompileFiles(SrcFileWrapper srcFileWrapper) {
        List<File> files = getCompileFileList(srcFileWrapper);
        return files.stream()
                .map(CompileFileWrapper::new)
                .collect(Collectors.toList());
    }

    protected List<File> getCompileFileList(SrcFileWrapper srcFileWrapper) {
        return Collections.singletonList(srcFileWrapper.getFile());
    }

    @Override
    public List<TargetFileWrapper> getTargetFiles(CompileFileWrapper compileFileWrapper) {
        return targetForderPaths.stream()
                .map(path -> new File(patchPluginProperties.getTargetPath() + path + compileFileWrapper.getFile().getName()))
                .map(TargetFileWrapper::new)
                .collect(Collectors.toList());
    }
}
